from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from user import models,userser
from django.db.models import Q
# Create your views here.
from utils.jwttool import tooljwt

class LoginByPsw(APIView):
    def post(self,request):
        account = request.data.get('account') # 账号  有可能是邮箱 手机号 用户名
        password = request.data.get('password')
        query = Q(username=account) | Q(email=account)| Q(phone=account)
        info = models.UserModel.objects.filter(query).first()
        if not info:
            return Response({'msg':"账号不存在","code":400})
        # 账号存在  判断密码是否正确
        if (info.password == password):
            # 根据info.id生成一个token
            token = tooljwt.get_token(info.id)
            return Response({'MSG':"登录成功","code":200,"data":{"token":token,"userid":info.id}})
        else:
            return Response({'msg':"密码错误","code":400})
